import game from "./game";
import score from "./score";

const { ccclass, property } = cc._decorator;
//此类实现敌人向下移动单节点
@ccclass
export default class target extends cc.Component {

    private canvas: cc.Node = null;
    private label: cc.Label = null;
    private health: number = 0;
    score: number = 0;
    speed: number = 100;
    private originPos: cc.Vec3; //预制体原本的位置
    onLoad() {
        this.canvas = cc.find('Canvas');
        this.score = 1 + Math.floor(Math.random() * 10);
        this.label = this.node.children[0].getComponent(cc.Label);
        this.health = this.score;
        this.healthLive();
        // this.originPos = this.node.position;
    }

    protected onEnable(): void {
        // this.node.setPosition(this.originPos);
    }

    healthLive() {
        this.label.string = this.health + '';
    }
    //碰撞回调函数。击中一次使生命值减一
    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        this.health -= 1;
        this.healthLive();
        if (this.health <= 0) {
            this.diss()
            score.i.addScore(this.score);
        }
    }
    update(dt) {
        let step = this.speed * dt;
        this.node.y -= step;
        if (this.node.y < this.canvas.height / 2 - 500) {
            this.diss();
          
        }
    }
    diss() {
        game.inst.ondestroyTarget(this.node);
        // console.log("敌人销毁超出范围")
    }
}
